﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PizzaService.Controller;
using PizzaService.View;
using PizzaService.Entities;

namespace PizzaService
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // if the window height is greater than the screen height
            if (System.Windows.SystemParameters.PrimaryScreenHeight < this.Height)
                this.Height = System.Windows.SystemParameters.PrimaryScreenHeight - 100;

            // if the window width is greater than the screen height
            if (System.Windows.SystemParameters.PrimaryScreenWidth < this.Width)
                this.Height = System.Windows.SystemParameters.PrimaryScreenWidth - 200;

            this.Loaded += MainWindow_Loaded;
        }

        /// <summary>
        /// Window Loaded event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.Hide();

            App.Controller.ExceptionEv += new PizzaController.ExceptionEventHandler(Controller_ExceptionEv);
            DataContext = App.Controller.MainViewModel;
            //App.Controller.GenerateModel();

            //
            // Show Login window on app startup
            LoginWindow loginWindow = new LoginWindow();
            loginWindow.Show();
            loginWindow.Closed += loginWindow_Closed;
        }

        /// <summary>
        /// Event for LoginWindow closed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void loginWindow_Closed(object sender, EventArgs e)
        {
            this.Show();
            this.Activate();
        }

        /// <summary>
        /// Exception Handling
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Controller_ExceptionEv(object sender, Events.ExceptionEvent e)
        {
            MessageBox.Show(e.ExceptionText);
        }

        /// <summary>
        /// Login Menuitem click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            Window login = new LoginWindow();
            login.Show();
        }

        /// <summary>
        /// Close Menuitem click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Logout Menuitem click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            App.Controller.Logout();
        }

        /// <summary>
        /// Open the EditCustomer Window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void customers_MouseDoubleClick_1(object sender, MouseButtonEventArgs e)
        {
            if (customers.SelectedItem != null)
            {
                Kunde temp = (Kunde)customers.SelectedItem;

                App.Controller.OpenEditCustomers(App.Controller.MainViewModel.Kunden.FirstOrDefault(p => p.ID == temp.ID));
            }
        }

        /// <summary>
        /// Closed Event for the MainWindow
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed_1(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        /// <summary>
        /// New Customer clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            App.Controller.CreateNewCustomer();
        }

        /// <summary>
        /// Delete Customer click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (customers.SelectedItem != null)
            {
                Kunde temp = (Kunde)customers.SelectedItem;

                App.Controller.DeleteCustomer(temp);
            }
        }

        /// <summary>
        /// Food Listview Item Clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListViewBtnClicked(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is Button)
            {
                var btn = (e.OriginalSource as Button);

                // get ListViewItem from Button DataContext
                var item = btn.DataContext;

                // now add the item to the other list

                App.Controller.OpenEditFood(item, btn.Name);
            }
        }

        /// <summary>
        /// Remove Food from order
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RemoveFood_Click(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is Button)
            {
                var btn = (e.OriginalSource as Button);

                // get ListViewItem from Button DataContext
                var item = btn.DataContext;

                // remove the item from the list

                App.Controller.RemoveFood(item);
            }
        }

        /// <summary>
        /// Assign customer to the actual order
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAssignToOrdner_Click_1(object sender, RoutedEventArgs e)
        {
            if (customers.SelectedItem != null)
            {
                Kunde temp = (Kunde)customers.SelectedItem;

                App.Controller.AssignCustomerToOrder(temp);

                MainTabControl.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// Choose a customer (switch to customers tab)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnChooseCustomer_Click_1(object sender, RoutedEventArgs e)
        {
            MainTabControl.SelectedIndex = 1;
        }

        /// <summary>
        /// /reset order
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnResetOrder_Click_1(object sender, RoutedEventArgs e)
        {
            App.Controller.ResetOrder();
        }

        /// <summary>
        /// complete the order and save it to db
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCompleteOrder_Click_1(object sender, RoutedEventArgs e)
        {
            App.Controller.CompleteOrder();
        }

        /// <summary>
        /// unassign customer from order
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnUnChooseCustomer_Click_1(object sender, RoutedEventArgs e)
        {
            App.Controller.UnassignCustomerFromOrder();
        }

        /// <summary>
        /// open an already completed order
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void orders_MouseDoubleClick_1(object sender, MouseButtonEventArgs e)
        {
            if (orders.SelectedItem != null)
            {
                App.Controller.OpenShowOrder(orders.SelectedItem as Bestellung);
            }
        }

        /// <summary>
        /// Menu Item for AboutBox clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_4(object sender, RoutedEventArgs e)
        {
            AboutBox about = new AboutBox();
            about.Show();
        }

        /// <summary>
        /// Menu item for Pizza erstellen clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_5(object sender, RoutedEventArgs e)
        {
            App.Controller.OpenCreateNewPizzaWindow();
        }

        /// <summary>
        /// Menu item for Speise löschen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            string selectedTabName = (tabSpeisen.SelectedItem as TabItem).Name;

            if (MessageBox.Show(String.Format("Dies wird die selektierte Speise im aktuell ausgewählten Tab ({0}) löschen", (tabSpeisen.SelectedItem as TabItem).Header), "Sicher?", MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes)
            {
            switch (selectedTabName)
            {
                case "tabItemPizzen":
                    App.Controller.DeleteFood(pizzaOrder.SelectedItem);
                    break;
                case "tabItemPasta":
                    App.Controller.DeleteFood(pastaOrder.SelectedItem);
                    break;
                case "tabItemSalat":
                    App.Controller.DeleteFood(salatOrder.SelectedItem);
                    break;
            }
            }
        }
    }
}
